package qp.src.app.client.widget;

import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public abstract class PagePopup extends DialogBox {
	
	
	private PageMode popupMode; // จะต้องมีค่าเสมอ
	  private Object popupParameter;
	  private PopupListener popupListener;

	  public PagePopup() {
	    super();
	    setGlassEnabled(true);
	    setAnimationEnabled(true);
	    
	    setText( getPopupTitle());
	    
	  }

	  public void init() {
		  
	    VerticalPanel dialogContents = new VerticalPanel();
	    dialogContents.setSpacing(2);
	    //dialogContents.set
	    dialogContents.setWidth("300");
	    
	    internalInitPage(dialogContents);
		setWidget(dialogContents);

	  }

	  //****************************************************************************

	  protected abstract void internalInitPage(VerticalPanel layout);

	  protected abstract String getPopupTitle();

	  protected abstract void onPageOpen();

	  protected abstract void onPageClose();

	  //****************************************************************************

	  protected final PageMode getPopupMode() {
	    return popupMode;
	  }

	  protected final Object getPopupParameter() {
	    return popupParameter;
	  }

	  //****************************************************************************

	  public void openPopup() {
	    openPopup(null, null, null);
	  }

	  public void openPopup(PageMode popupMode) {
	    openPopup(popupMode, null, null);
	  }

	  public void openPopup(PageMode popupMode, Object popupParameter) {
	    openPopup(popupMode, popupParameter, null);
	  }

	  public final void openPopup(PageMode popupMode, Object popupParameter, PopupListener popupListener) {
	    this.popupMode = popupMode;
	    this.popupParameter = popupParameter;
	    this.popupListener = popupListener;

	    //setTitle( getPopupTitle() );
	    
	    center();
	    show();
	    onPageOpen();
	    
	  }

	  //****************************************************************************

	  protected final void closePopup() {
	    hide();
	  }

	  protected void closePopup(Object result) {
	    onPageClose();
	    closePopup();

	    if (popupListener != null) {
	      popupListener.onReturn(result);
	    }
	  }
}
